package tue.cse.advanceddatabases.project.xml.track;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class Track {

	private Element element;

	public Track() {
		this.element = DocumentHelper.createElement("track");
	}

	public Track(Element element) {
		this.element = element;
	}

	public String getId() {
		return element.attributeValue("id");
	}

	public void setId(String value) {
		if (element.attribute("id") == null)
			element.addAttribute("id", value);
		else
			element.attribute("id").setValue(value);
	}

	public String getArtistId() {
		return element.elementText("artist_id");
	}

	public void setArtistId(String value) {
		if (element.element("artist_id") == null)
			element.addElement("artist_id").setText(value);
		else
			element.element("artist_id").setText(value);
	}

	public String getAlbumId() {
		return element.elementText("album_id");
	}

	public void setAlbumId(String value) {
		if (element.element("album_id") == null)
			element.addElement("album_id").setText(value);
		else
			element.element("album_id").setText(value);
	}

	public String getName() {
		return element.elementText("name");
	}

	public void setName(String value) {
		if (element.element("name") == null)
			element.addElement("name").setText(value);
		else
			element.element("name").setText(value);
	}

	public String getSeconds() {
		return element.elementText("seconds");
	}

	public void setSeconds(String value) {
		if (element.element("seconds") == null)
			element.addElement("seconds").setText(value);
		else
			element.element("seconds").setText(value);
	}

	public String getYear() {
		return element.elementText("year");
	}

	public void setYear(String value) {
		if (element.element("year") == null)
			element.addElement("year").setText(value);
		else
			element.element("year").setText(value);
	}

	public String getDownloadUrl() {
		return element.elementText("url");
	}

	public void setDownloadUrl(String value) {
		if (element.element("url") == null)
			element.addElement("url").setText(value);
		else
			element.element("url").setText(value);
	}

	public String getLaunchdate() {
		return element.elementText("launchdate");
	}

	public void setLaunchdate(String value) {
		if (element.element("launchdate") == null)
			element.addElement("launchdate").setText(value);
		else
			element.element("launchdate").setText(value);
	}

	public String getDateCreated() {
		return element.elementText("date_created");
	}

	public void setDateCreated(String value) {
		if (element.element("date_created") == null)
			element.addElement("date_created").setText(value);
		else
			element.element("date_created").setText(value);
	}

	public String getRating() {

		if ( (element.elementText("rating") == null) || 
			 (element.elementText("rating-count")==null) ||
			 (element.elementText("rating").length() <= 0) || 
			 (element.elementText("rating-count").length() <=0 ) ||
			  "0".equalsIgnoreCase(element.elementText("rating")) ||
			  "0".equalsIgnoreCase(element.elementText("rating-count"))) {
			return "no";
		}

		long rating = Long.valueOf(element.elementText("rating")).longValue();
		long ratingCount = Long.valueOf(element.elementText("rating-count"))
				.longValue();

		long rateValue = (long) Math.ceil(rating / ratingCount);

		return String.valueOf(rateValue);

	}

	public String getUrl() {
		return element.elementText("url");
	}

	public void setUrl(String value) {
		if (element.element("url") == null)
			element.addElement("url").setText(value);
		else
			element.element("url").setText(value);
	}
	
	public String getGenre(){
	  	return element.elementText("genre");
	}
	public void setGenre(String value){
	  	if (element.element("genre") == null)
	  	  	element.addElement("genre").setText(value);
	  	else
	  	  	element.element("genre").setText(value);
	}
	
	public String getCommentCount(){
		return element.elementText("comment-count");
	}
	
	public void setCommentCount(String value) {
		// do nothing. it is automatic done when commenting.
	}	
}
